using System;
using System.Collections;
using System.Text;
using System.Data;
using GeneralLibs;

namespace Lists
{
    public class KetChuyen : DoiTuong
    {
        #region SoTT property
        public int SoTT
        {
            get
            {
                return (int)_DataRow["SoTT"];
            }
            set
            {
                _DataRow["SoTT"] = value;
            }
        }
        #endregion

        #region TaiKhoanDi property
        public TaiKhoan TaiKhoanDi
        {
            get
            {
                if (_DataRow["TaiKhoanDiID"] is Guid)
                    return DanhMucTaiKhoan.Instance.GetByProperty("ID", _DataRow["TaiKhoanDiID"]);
                else
                    return null;
            }
            set
            {
                if (value is TaiKhoan)
                    _DataRow["TaiKhoanDiID"] = value.ID;
                else
                    _DataRow["TaiKhoanDiID"] = DBNull.Value;
            }
        }
        #endregion

        #region TaiKhoanDen property
        public TaiKhoan TaiKhoanDen
        {
            get
            {
                if (_DataRow["TaiKhoanDenID"] is Guid)
                    return DanhMucTaiKhoan.Instance.GetByProperty("ID", _DataRow["TaiKhoanDenID"]);
                else
                    return null;
            }
            set
            {
                if (value is TaiKhoan)
                    _DataRow["TaiKhoanDenID"] = value.ID;
                else
                    _DataRow["TaiKhoanDenID"] = DBNull.Value;
            }
        }
        #endregion

        #region DienGiai property
        public string DienGiai
        {
            get
            {
                return (string)_DataRow["DienGiai"];
            }
            set
            {
                _DataRow["DienGiai"] = value;
            }
        }
        #endregion

        #region Chon property
        public bool Chon
        {
            get
            {
                return (bool)_DataRow["Chon"];
            }
            set
            {
                _DataRow["Chon"] = value;
            }
        }
        #endregion

        #region KieuKetChuyen property
        public KieuKetChuyen KieuKetChuyen
        {
            get
            {
                return (KieuKetChuyen)(int)_DataRow["KieuKetChuyen"];
            }
            set
            {
                _DataRow["KieuKetChuyen"] = (int)value;
            }
        }
        #endregion

        public KetChuyen(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucKetChuyen : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucKetChuyen _Instance;
        public static DanhMucKetChuyen Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucKetChuyen();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                    _Instance.DataView.Sort = "SoTT";
                }
                return _Instance;
            }
        }
        #endregion
        #region override TableName property
        public override string TableName
        {
            get
            {
                return "KetChuyen";
            }
        }
        #endregion 

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new KetChuyen((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public KetChuyen this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new KetChuyen(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public KetChuyen NewDoiTuong()
        {
            return new KetChuyen(DataTable.NewRow());
        }

        public KetChuyen GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new KetChuyen(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public KetChuyen GetByID(Guid objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucKetChuyen GetAll()
        {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
